const log4js = require('../../../../utils/logUtils');
const deviceCache = require("../../../../db/deviceCache");
const logger = log4js.getLogger('sip');

const TAG = "[recordInfoHandler]";
module.exports={
    handle(deviceInfo, response){
        const SN = response.SN[0];
        const recordList = response.RecordList[0].Item;
        logger.info(TAG, "[录像信息] deviceInfo=", deviceInfo.deviceid, SN, recordList);
        // 遍历 recordList，把每个path里的 下级deviceId换成上级deviceId

        recordList.forEach(item=>{
            const path = item.FilePath[0];
            const arr = path.split('_');
            item.downDeviceId = arr[0];
            arr[0] = deviceInfo.deviceid;
            item.FilePath[0] = arr.join('_');
        });
        if(recordList.length>0){
            deviceCache.recordList[SN+''] = {
                time: new Date(),
                items: recordList
            };
        }
        // 把20秒前的数据清理掉
        for(let key in deviceCache.recordList){
            const item = deviceCache.recordList[key];
            if(new Date().getTime() - item.time.getTime() > 20000){
                delete deviceCache.recordList[key];
            }
        }

    }
}
